from sqlalchemy import Column, String, Integer, DateTime, true
from sqlalchemy.dialects.mysql import LONGTEXT
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from init.mysql import engine

Base = declarative_base()


class TaskInfo(Base):
    __tablename__ = 'task_info'
    id = Column(String(20), autoincrement="auto", primary_key=True)
    name = Column(String(50), default='', name='name', comment='名称')
    url = Column(String(100), name='url', comment='请求url1')
    url2 = Column(String(100), name='url2', comment='备份请求url2')
    period = Column(Integer, default=0, name="period", comment="更新周期")
    request_frequency = Column(Integer, default=0, name="request_frequency", comment="最请求频率")
    comment = Column(LONGTEXT, nullable=True, comment="接口说明")
    format = Column(LONGTEXT, nullable=True, comment="返回数据格式说明")
    update_time = Column(String(100), comment="更新时间点")
    start_time = Column(DateTime, comment="任务开始时间")
    end_time = Column(DateTime, nullable=true, comment="为空时不限制")
    count = Column(Integer, default=0, name="count", comment="总执行次数")

    def __init__(self,id, name, url, url2, period, request_frequency, comment, format, update_time, start_time, end_time,
                 count=0):
        self.id = id,
        self.name = name
        self.url = url
        self.url2 = url2
        self.period = period
        self.request_frequency = request_frequency
        self.comment = comment
        self.format = format
        self.update_time = update_time
        self.start_time = start_time
        self.end_time = end_time
        self.count = count

    @staticmethod
    def delete(id_):
        session = sessionmaker(bind=engine)()
        session.query(TaskInfo).delete(id_)
        session.commit()
        session.close()

    def insert(self):
        session = sessionmaker(bind=engine)()
        session.add(self)
        session.commit()
        session.close()

    def update(self):
        session = sessionmaker(bind=engine)()
        taskInfo = session.query(TaskInfo).filter_by(id=self.id).first()
        taskInfo.name = self.name
        taskInfo.url = self.url
        taskInfo.url2=self.url2
        taskInfo.period = self.period
        taskInfo.request_frequency = self.request_frequency
        taskInfo.comment = self.comment
        taskInfo.format = self.format
        taskInfo.update_time = self.update_time
        taskInfo.start_time = self.start_time
        taskInfo.count = self.count
        session.commit()
        session.close()

    def serialize(self):
        return {
            'id': self.id,
            'name': self.name,
            'url': self.url,
            'url2': self.url2,
            'period': self.period,
            'request_frequency': self.request_frequency,
            'comment': self.comment,
            'format': self.format,
            'update_time': self.update_time,
            'start_time': self.start_time,
            'count': self.count,
        }


Base.metadata.create_all(engine)
